//
//  InvestEventViewController.m
//  investevent
//
//  Created by jiangshao on 16/3/5.
//  Copyright © 2016年 com.scj.investevent. All rights reserved.
//

#import "InvestEventViewController.h"
#import "InvestEventDao.h"
#import "EventViewCell.h"
#import "InvestService.h"
#import "TianMaoDao.h"

@interface InvestEventViewController (){
    NSMutableArray *investdatas;
    BOOL _isLoadingData;
    InvestEventDao *investEventDao;
    TianMaoDao *tianMaoDao;
}

@end

@implementation InvestEventViewController{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    investdatas=[[NSMutableArray alloc]init];
    investEventDao=[[InvestEventDao alloc]initWithDelagate:self];
    
//    1668434515
//    tianMaoDao=[[TianMaoDao alloc]initWithDelagate:self];
//    [tianMaoDao getCoupon:@"1668434515"];
    
    _tableView.contentInset=UIEdgeInsetsMake(0, 0, 49, 0);
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor=[UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator=NO;
    [_tableView addPullToRefreshWithActionHandler:^{
        if (!_isLoadingData) {[self reloadData];}
    }];
    [_tableView.pullToRefreshView setTitle:@"下拉刷新" forState:SVPullToRefreshStateAll];
    [_tableView.pullToRefreshView setTitle:@"松开刷新" forState:SVPullToRefreshStateTriggered];
    [_tableView.pullToRefreshView setTitle:@"正在加载" forState:SVPullToRefreshStateLoading];
    
    [self reloadData];

    [_tableView registerNib:[UINib nibWithNibName:@"EventViewCell" bundle:nil] forCellReuseIdentifier:@"EventViewCell"];
    // Do any additional setup after loading the view.
}


-(void)reloadData{
    [investEventDao getInvestEvent:self.date];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)getRemotedata:(NSMutableDictionary*)dic{
    
    if ([dic objectForKey:@"getInvestEvent"]) {
        [_tableView.pullToRefreshView stopAnimating];
        id investobject=[dic objectForKey:@"getInvestEvent"];
        investdatas=[investobject objectForKey:@"data"] ;
        
//        [investdatas addObject:[[investobject objectForKey:@"data"] lastObject]];
        [_tableView reloadData];
        
        
        
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        unsigned int unitFlags = NSDayCalendarUnit;
        
        int currentDay=300;
        int item=0;
        @try {
            for (int i=0; i<investdatas.count; i++) {
                NSDictionary *data=investdatas[i];
                NSString *date=[data objectForKey:@"date"];
                NSDateComponents *comps = [gregorian components:unitFlags fromDate:[NSDate dateFromString:date format:@"yyyy-MM-dd"]  toDate:[NSDate date]  options:0];
               int   days = [comps day];
                days=abs(days);
                if (days<currentDay) {
                    currentDay=days;
                    item=i;
                }
            }
//            NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:0];
//            [_tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }
        //        NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:0];
//        [_tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return investdatas.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=investdatas[indexPath.row];
    return [InvestService getInvestCellHeight:dict];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict=investdatas[indexPath.row];
    EventViewCell *eventViewCell= (EventViewCell *)[tableView dequeueReusableCellWithIdentifier:@"EventViewCell"];
    eventViewCell.selectionStyle=UITableViewCellSelectionStyleNone;
    [eventViewCell setContent:dict];
    return eventViewCell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
